package work.chief.service.impl;

import work.chief.dao.OrdersDao;
import work.chief.dao.ProductDao;
import work.chief.dao.TrolleyDao;
import work.chief.dao.impl.OrdersDaoImpl;
import work.chief.dao.impl.ProductDaoImpl;
import work.chief.dao.impl.TrolleyDaoImpl;
import work.chief.entity.Orders;
import work.chief.entity.Product;
import work.chief.entity.Trolley;
import work.chief.service.OrdersService;
import work.chief.utils.VerifyCodeUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    private ProductDao productDao = new ProductDaoImpl();
    private TrolleyDao trolleyDao = new TrolleyDaoImpl();
    private OrdersDao ordersDao = new OrdersDaoImpl();

    @Override
    public int addOrders(int uid, String tid, HttpServletRequest request) {
        int num = 0;

        try {
            //调用工具类生成一个16位的订单号
            String order_number = VerifyCodeUtils.generateOrdersCode(16);

            //记录订单数和总价格
            int count = 0;
            double sum = 0;

            String[] arrays = tid.split(",");
            if (arrays != null && arrays.length > 0) {
                for (String s : arrays) {
                    //查找购物车，根据订单号
                    Trolley trolley = trolleyDao.selectByTid(Integer.parseInt(s));
                    //更新购物车，根据tid，orders_number
                    trolleyDao.updateTrolley(trolley.getTid(), order_number);
                    //查找商品，根据pid
                    Product product = productDao.selectByPid(trolley.getPid());

                    //累计订单数和价格
                    count += trolley.getNumber();

                    sum += Double.parseDouble(product.getPrice()) * trolley.getNumber();
                }
            }


            //订单对象
            Orders orders = new Orders();
            //设置订单
            orders.setUid(uid);
            orders.setCount_number(count);
            orders.setState(2);
            orders.setOrders_number(order_number);
            orders.setCreate_time(new Date());
            orders.setSum_price(sum);

            //加入订单
            num = ordersDao.addOrders(orders);

            request.getSession().setAttribute("sum",sum);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Orders> selectByUid(int uid) {
        //查询所有订单信息，根据uid
        List<Orders> ordersList = ordersDao.selectByUid(uid);

        //遍历订单信息
        if (ordersList!=null && ordersList.size()>0){
            for (Orders orders:ordersList){
                String orders_number = orders.getOrders_number();
                //查询购物车
//                System.out.println(orders_number+"\n");
//                List<Trolley> trolleyList = trolleyDao.selectByOrders_number(orders_number);
                List<Trolley> trolleyList = trolleyDao.selectByOrders_number(orders_number);
                orders.setTrolleyList(trolleyList);
//                System.out.println(trolleyList+"\n");
                //遍历购物车
                if (trolleyList!=null && trolleyList.size()>0){
                    for (Trolley trolley:trolleyList){
                        int pid= trolley.getPid();
                        //查询商品
                        Product product = productDao.selectByPid(pid);
//                        System.out.println(product+"\n");

                        //给trolley设置Product值
                        trolley.setProduct(product);

                        System.out.println(trolley.getProduct().getName());
                    }
                }

            }
        }

        return ordersList;
    }
}
